/*------------------------------------------------------
                hou_elearningGEN beta v1.0
Author: xtrung.net@gmail.com
Write On: 04/27/2008
Create On:4/13/2012 11:23:02 AM
------------------------------------------------------*/
using hou_elearning.DataAccess.Base;
using hou_elearning.Entity;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections.Generic;
using System.Collections;
using System.Web;

namespace hou_elearning.DataAccess
{    
    public class NguoidungDAL : SqlProvider<NguoidungEntity>
    {
        static NguoidungDAL()
        {
            InitReader();
        }
        protected static void InitReader()
        {
            getFromReader=delegate(SqlDataReader dr)
            {
                NguoidungEntity entity = new NguoidungEntity();
				entity.PK_iNguoidungID = Int64.Parse("0"+dr["PK_iNguoidungID"].ToString());
				entity.sTendangnhap = dr["sTendangnhap"].ToString();
				entity.sMatkhau = dr["sMatkhau"].ToString();
				entity.sHoten = dr["sHoten"].ToString();
				entity.sEmail = dr["sEmail"].ToString();
				entity.sSodienthoai = dr["sSodienthoai"].ToString();
				entity.dNgaysinh =String.IsNullOrEmpty(dr["dNgaysinh"].ToString())?DateTime.Now:DateTime.Parse(dr["dNgaysinh"].ToString());
				entity.bGioitinh =String.IsNullOrEmpty(dr["bGioitinh"].ToString())?false:Boolean.Parse(dr["bGioitinh"].ToString());
				entity.sDiachi = dr["sDiachi"].ToString();
                entity.sGhichu = dr["sGhichu"].ToString();
                try
                {
                    entity.bAnhthe = (byte[])(dr["bAnhthe"]);
                }
                catch {}
                return entity;
            };
        }
        public static NguoidungEntity GetOne(Int64 PK_iNguoidungID)
        {
            string cmdName = "spNguoidung_GetByPK";
            SqlParameter p = new SqlParameter("@PK_iNguoidungID", PK_iNguoidungID);
            NguoidungEntity entity = GetOne(cmdName, p);
            return entity;
        }
        public static List<NguoidungEntity> GetAll()
        {
            string cmdName = "spNguoidung_Get";
            return GetList(cmdName);
        }
        public static List<NguoidungEntity> GetTopByVote()
        {
            string cmdName = "spGiangvien_GetTopByVote";
            return GetList(cmdName);
        }
        public static int Add(NguoidungEntity entity)
        {
            string cmdName = "spNguoidung_Insert";
            return Run(cmdName,true,initParams(entity));

        }
        public static bool Edit(NguoidungEntity entity)
        {
            string cmdName = "spNguoidung_Update";
            return Run(cmdName,false,initParams(entity))>0;
        }
        public static bool Remove(Int64 PK_iNguoidungID)
        {
            string cmdName = "spNguoidung_Delete";
            SqlParameter p = new SqlParameter("@PK_iNguoidungID", PK_iNguoidungID);
            return Run(cmdName,false,p)>0;
        }
        #region private
        private static SqlParameter[] initParams(NguoidungEntity entity)
        {
            SqlParameter[] p = new SqlParameter[11];
			p[0] = new SqlParameter("@PK_iNguoidungID", entity.PK_iNguoidungID);
			p[1] = new SqlParameter("@sTendangnhap", entity.sTendangnhap);
			p[2] = new SqlParameter("@sMatkhau", entity.sMatkhau);
			p[3] = new SqlParameter("@sHoten", entity.sHoten);
			p[4] = new SqlParameter("@sEmail", entity.sEmail);
			p[5] = new SqlParameter("@sSodienthoai", entity.sSodienthoai);
			p[6] = new SqlParameter("@dNgaysinh", entity.dNgaysinh);
			p[7] = new SqlParameter("@bGioitinh", entity.bGioitinh);
			p[8] = new SqlParameter("@sDiachi", entity.sDiachi);
            p[9] = new SqlParameter("@sGhichu", entity.sGhichu);
			p[10] = new SqlParameter("@bAnhthe", entity.bAnhthe);
            return p;
        }
        #endregion
       
    }
}